/* 地图参考https://gallery.echartsjs.com/editor.html?c=xryHrMdE2M*/
import {
  mapStyleJson,
  configurationInformation
} from '../configure.js'
import {
  viLine,
  viBar,
  viDoubleBar,
  viProgress,
  viPlate,
  viMap,
  viBalance,
  viDetailed,
  viInvoiceCollection,
  viPopupProfit,
  viPopupProgress,
  viPopupBalance,
  viPopupDetail,
  viPopupDoubleBar,
} from '@/components/visualization/index'

import request from '@/utils/request'
import Qs from 'qs'
export default {
  components: {
    viLine,
    viBar,
    viDoubleBar,
    viProgress,
    viPlate,
    viMap,
    viBalance,
    viDetailed,
    viInvoiceCollection,
    viPopupProfit,
    viPopupProgress,
    viPopupBalance,
    viPopupDetail,
    viPopupDoubleBar,
  },
  data() {
    return {
      mapStyleJson,
      configurationInformation,
      monthList: [
        '01',
        '02',
        '03',
        '04',
        '05',
        '06',
        '07',
        '08',
        '09',
        '10',
        '11',
        '12'
      ],
      deptList: [],
      deptId: -1,
      projectList: [],
      projectId: null,
      mapList: [], // 地图信息
      titleList: {},
      balanceList: [], // 资金账户余额
      advanceMonthList: [],
      todayList: [], // 当日明细
      invoiceAndCollectionMonthList: [], // 开票与收款
      accoutTagList: [], //所有公司列表
      year: "",
      month: "",
      newType: '1',
      newType1: '1',
      newType2: '1',
    }
  },
  async created() {
    this.gettimes()
    this.changeDept(this.deptId)

  },
  mounted() {},
  methods: {
    getProjectInfoByDeptId() {
      // this.isShowContent=false
      //获取项目列表
      return new Promise(resolve => {
        request({
          url: "/system/screen/getProjectInfoByDeptId",
          method: "post",
          headers: {
            "content-type": "application/x-www-form-urlencoded"
          },
          data: Qs.stringify({
            deptId: this.deptId,
          }),
        }).then((res) => {
          this.projectList = res.data;
          if (res.data && res.data.length > 0) {
            //默认取第一个
            this.projectId = res.data[0].deptId
          }
          resolve(res)
        });
      })
    },
    gettimes() {
      const time = new Date();
      const year = time.getFullYear();
      const month = time.getMonth() + 1;
      const day = time.getDate();
      this.month = month
      this.year = `${year}`
    },
    async changeDept(val) {
      this.deptId = val
      await this.screenRegion() // 获取地图地区列表
      await this.getProjectInfoByDeptId()
      this.screenDistribution() // 获取地图坐标信息
      this.screenTitle() // 标题qu

      this.screenAdvanceMonth() // 垫资情况
      this.screenTodayFlow() // 当日明细
      // this.screenInvoiceAndCollectionMonth() // 开票与收款
      this.changeRadiodrawProfit() // 利润
      this.screenSpeedList()
      this.$nextTick(() => {
        this.drawInvoiceCollection1() //开票与收款
        this.drawProfit()
        this.drawAdvanceSelect()
      })
      this.accoutTagList = await this.getorgList() // 获取所有公司列表
      this.screenBalance() // 资金账户余额
      this.$refs.income.require({
        deptId: this.deptId,
        projectId: this.projectId,
      })
    },
    async changeProject(val) {
      this.projectId = val
      this.screenDistribution() // 获取地图坐标信息
      this.screenTitle() // 标题qu

      this.screenAdvanceMonth() // 垫资情况
      this.screenTodayFlow() // 当日明细
      // this.screenInvoiceAndCollectionMonth() // 开票与收款
      this.changeRadiodrawProfit() // 利润
      this.screenSpeedList()
      this.$nextTick(() => {
        this.drawInvoiceCollection1() //开票与收款
        this.drawProfit()
        this.drawAdvanceSelect()
      })
      this.accoutTagList = await this.getorgList() // 获取所有公司列表
      this.screenBalance() // 资金账户余额
      this.$refs.income.require({
        deptId: this.deptId,
        projectId: this.projectId,
      })
    },
    getorgList() {
      return new Promise((resolve, reject) => {
        request({
          url: '/system/accountTag/list',
          method: 'post',
          headers: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: Qs.stringify({
            category: 2,
            projectId: this.projectId,
          })
        }).then(res => {
          res.rows = res.rows.length > 0 ?
            res.rows.filter(item => item.parentId === 108) : []
          console.log(666, res.rows)
          resolve(res.rows)
        })
      })
    },
    screenRegion() {
      request({
        url: '/system/screen/region',
        method: 'post'
      }).then(res => {
        this.deptList = res.data
      })
    },
    screenDistribution(deptId = this.deptId) {
      request({
        url: '/system/screen/distribution',
        method: 'post',
        headers: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: Qs.stringify({
          deptId,
          // projectId: this.projectId,
        })
      }).then(res => {
        this.mapList = res.data.filter(item => item.center)
      })
    },
    screenTitle(deptId = this.deptId) {
      request({
        url: '/system/screen/title',
        method: 'post',
        headers: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: Qs.stringify({
          deptId,
          projectId: this.projectId,
        })
      }).then(res => {
        /**
         * saleAmount  销售额
         * currMonthIncome  本月新增收入
         * currYearIncome  今年累计收入
         * yearProfile  今年累计利润
         */
        this.titleList = res.data
      })
    },
    screenBalance(deptId = this.deptId) {
      request({
        url: '/system/screen/balance',
        method: 'post',
        headers: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: Qs.stringify({
          deptId,
          projectId: this.projectId,
        })
      }).then(res => {
        /**
         * saleAmount  销售额
         * currMonthIncome  本月新增收入
         * currYearIncome  今年累计收入
         * yearProfile  今年累计利润
         */
        // this.balanceList = res.data
        this.$refs.balance.draw(res.data)
      })
    },
    drawAdvanceSelect(type = this.newType2 || '1') {
      this.newType2 = type
      if (type === '1') {
        this.screenAdvanceMonth(this.deptId, type)
      } else {
        this.screenAdvanceMonthAdvanceYear(this.deptId, type)
      }
    },
    screenAdvanceMonth(deptId = this.deptId, type) {
      request({
        url: '/system/screen/AdvanceMonth',
        method: 'post',
        headers: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: Qs.stringify({
          deptId,
          projectId: this.projectId,
        })
      }).then(res => {
        if (!res.data || res.data.length === 0) {
          this.advanceMonthList = []
          return
        }
        const advanceMonthList = []
        this.monthList.forEach((item, i) => {
          advanceMonthList.push({
            amount: 0,
            deptName: null,
            month: item,
            year: null
          })
          res.data.forEach(el => {
            if (item === el.month) {
              advanceMonthList[i] = el
            }
          })
        })

        this.advanceMonthList = advanceMonthList.map(
          item => item.amount / 10000 || 0
        )
        this.drawAdvance(type)
      })
    },
    screenAdvanceMonthAdvanceYear(deptId = this.deptId, type) {
      request({
        url: '/system/screen/AdvanceYear',
        method: 'post',
        headers: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: Qs.stringify({
          deptId,
          projectId: this.projectId,
        })
      }).then(res => {
        const x = []
        const y = []
        let keys = Object.keys(res.data)
        if (keys.length > 0) {
          keys = keys.sort()
          keys.forEach(item => {
            x.push(item)
            y.push((res.data[item] / 10000).toFixed(2) || 0)
          })
        }
        this.drawAdvance(type, x, y)
      })
    },
    screenInvoiceAndCollectionMonth(deptId = this.deptId) {
      request({
        url: '/system/screen/invoiceAndCollectionMonth',
        method: 'post',
        headers: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: Qs.stringify({
          deptId,
          year: this.year,
          projectId: this.projectId,
        })
      }).then(res => {
        const x = []
        const y1 = []
        const y2 = []
        for (let i = 0; i < 12; i++) {
          x.push(`${i + 1}月`)
        }
        for (const i in res.data) {
          const data = res.data[i].split('|')
          y1.push((Number(data[0]) / 10000).toFixed(4) || 0)
          y2.push((Number(data[1]) / 10000).toFixed(4) || 0)
        }
        this.drawInvoiceCollection('1', y1, y2, x)
      })
    },
    screenInvoiceAndCollectionYear(deptId = this.deptId) {
      request({
        url: '/system/screen/invoiceAndCollectionYear',
        method: 'post',
        headers: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: Qs.stringify({
          deptId,
          projectId: this.projectId,
        })
      }).then(res => {
        const x = []
        const y1 = []
        const y2 = []
        for (const i in res.data) {
          x.push(i)
          const data = res.data[i].split('|')
          y1.push((Number(data[0]) / 10000).toFixed(4) || 0)
          y2.push((Number(data[1]) / 10000).toFixed(4) || 0)
        }
        this.drawInvoiceCollection('2', y1, y2, x)
      })
    },
    drawInvoiceCollection1(type = this.newType || '1') {
      this.newType = type
      if (type === '1') {
        this.screenInvoiceAndCollectionMonth()
      } else {
        this.screenInvoiceAndCollectionYear()
      }
    },
    drawInvoiceCollection(type = '1', yData1, yData2, xData) {
      // 开票与收款
      console.log(this.configurationInformation)
      this.configurationInformation.invoiceCollection = {
        ...this.configurationInformation.invoiceCollection,
        xData,
        yData1,
        yData2
      }
      this.$refs.invoiceCollection.draw(
        this.configurationInformation.invoiceCollection
      )
    },
    screenMonthProfit(deptId = this.deptId) {
      request({
        url: '/system/screen/monthProfit',
        method: 'post',
        headers: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: Qs.stringify({
          deptId,
          year: this.year,
          projectId: this.projectId,
        })
      }).then(res => {
        const x = []
        const y = []
        for (const i in res.data) {
          x.push(`${i}月`)
          y.push((Number(res.data[i]) / 10000).toFixed(2) || 0)
        }
        this.drawProfit('1', x, y)
      })
    },
    screenYearProfit(deptId = this.deptId) {
      request({
        url: '/system/screen/yearProfit',
        method: 'post',
        headers: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: Qs.stringify({
          deptId,
          year: this.year,
          projectId: this.projectId,
        })
      }).then(res => {
        const x = []
        const y = []
        for (const i in res.data) {
          x.push(`${i}`)
          y.push((Number(res.data[i]) / 10000).toFixed(2) || 0)
        }
        this.drawProfit('2', x, y)
      })
    },
    drawProfit(type = '1', xData, yData) {
      // 利润
      console.log(this.configurationInformation)
      const barwidth = '24'
      this.configurationInformation.profit = {
        ...this.configurationInformation.profit,
        barwidth,
        xData,
        yData
      }
      console.log(this.configurationInformation.profit)
      this.$refs.profit.draw(this.configurationInformation.profit)
    },
    changeRadiodrawProfit(type = this.newType1 || '1') {
      this.newType1 = type
      if (type === '1') {
        this.screenMonthProfit()
      } else {
        this.screenYearProfit()
      }
    },
    drawAdvance(type = '1', x, y) {
      // 垫资
      console.log(this.configurationInformation)
      const xData = []
      const yData = []
      let barwidth = '24'
      if (type === '1') {
        // 年度
        for (let i = 0; i < 12; i++) {
          xData.push(`${i + 1}月`)
          yData.push(((Math.random() + 1) * 10).toFixed(2))
        }
        barwidth = '16'
        this.configurationInformation.advance = {
          ...this.configurationInformation.advance,
          barwidth,
          xData,
          yData: this.advanceMonthList
        }
        // this.$refs.advance.draw(this.configurationInformation.advance)
      } else {
        // 累计
        this.configurationInformation.advance = {
          ...this.configurationInformation.advance,
          barwidth,
          xData: x,
          yData: y
        }
        // this.$refs.advance.draw(this.configurationInformation.advance)
      }
    },
    screenTodayFlow() {
      request({
        url: 'system/screen/todayFlow',
        method: 'post',
        headers: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: Qs.stringify({
          deptId:this.deptId,
          projectId: this.projectId,
        })
      }).then(res => {
        this.todayList = res.data
      })
    },
    screenSpeedList(deptId = this.deptId) {
      request({
        url: '/system/screen/speedList',
        method: 'post',
        headers: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: Qs.stringify({
          deptId,
          year: this.year,
          month: this.month,
          projectId: this.projectId,
        })
      }).then(res => {
        const data = []
        if (res && res.data && res.data.length > 0) {
          // 1灰色圆圈 2激活色圆圈 3有背景色条纹
          // ["申请 type1", "外证 type2", "交税 type3", "开票 type4", "抵扣 type5"]
          res.data.forEach(item => {
            const deptName = item.deptName
            const infos = {
              type1: 1,
              type2: 1,
              type3: 1,
              type4: 1,
              type5: 1
            }
            if (item.infos && item.infos.length > 0) {
              item.infos.forEach(el => {
                if (el.speedName === '申请') {
                  if (el.speedAgreeDate) {
                    infos.type1 = 2
                  }
                  if (el.speedAgreeDate && el.speedSubmitDate) {
                    infos.type1 = 3
                  }
                } else if (el.speedName === '外证') {
                  if (el.speedAgreeDate) {
                    infos.type2 = 2
                  }
                  if (el.speedAgreeDate && el.speedSubmitDate) {
                    infos.type2 = 3
                  }
                } else if (el.speedName === '交税') {
                  if (el.speedAgreeDate) {
                    infos.type3 = 2
                  }
                  if (el.speedAgreeDate && el.speedSubmitDate) {
                    infos.type3 = 3
                  }
                } else if (el.speedName === '开票') {
                  if (el.speedAgreeDate) {
                    infos.type4 = 2
                  }
                  if (el.speedAgreeDate && el.speedSubmitDate) {
                    infos.type4 = 3
                  }
                } else if (el.speedName === '抵扣') {
                  if (el.speedAgreeDate) {
                    infos.type5 = 2
                  }
                  if (el.speedAgreeDate && el.speedSubmitDate) {
                    infos.type5 = 3
                  }
                }
              })
            }
            data.push({
              deptName,
              infos
            })
          })
        }
        console.log(data)
        this.$refs.progress.open(data)
      })
    }
  }
}
